package com.nolydake.swingnote.service.note;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import com.nolydake.swingnote.SwingNoteApplication;
import com.nolydake.swingnote.dao.NoteForderDao;
import com.nolydake.swingnote.model.Note;
import com.nolydake.swingnote.model.NoteForder;

/**
 * 基于数据库系统的实现
 * 
 * @author NOLY DAKE
 *
 */
public class NoteServiceImpl implements INoteService {

    public List<NoteForder> fillForderTreeRootNode(DefaultMutableTreeNode forderTreeRootNode) {
        // try {
        //
        // // 数据库连接
        // Connection connection = SwingNoteApplication.connection;
        // Statement statement = connection.createStatement();
        // statement.setQueryTimeout(30);
        //
        // statement.executeUpdate("drop table if exists dictionary");
        // statement.executeUpdate("create table dictionary (word string,p1
        // string,p2 string)");
        // statement.executeUpdate("insert into dictionary
        // values('one','wan','WAN')");
        // statement.executeUpdate("insert into dictionary
        // values('two','tu','TU')");
        //
        // ResultSet rs = statement.executeQuery("select * from dictionary");
        //
        // while (rs.next()) {
        // System.out.println(rs.getString("word") + rs.getString("p1") +
        // rs.getString("p2"));
        // }
        // } catch (SQLException ex) {
        // }

        NoteForderDao forderDao = new NoteForderDao();
        forderDao.setConnection(SwingNoteApplication.dataSource);

        try {
            List<NoteForder> result = forderDao.loadNoteForderTree();

            for (NoteForder noteForder : result) {
                DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(noteForder);
                forderTreeRootNode.add(node1);
            }

            return result;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

        ////////////
        // NoteForder noteForder = new NoteForder();
        // noteForder.id = temp.getAbsolutePath();
        // noteForder.name = temp.getName();
        // noteForder.lastUpdateTime = new Date(temp.lastModified());
        // result.add(noteForder);
        //
        //
        // tempNode = node1;
        //
        // fileNodeMap.put(temp, tempNode);
        // ////////////
        //
        // PreparedStatement stmt = connection.prepareStatement("");
        //
        // final List<NoteForder> result = new ArrayList<NoteForder>();
        //
        // String notesPath = FileUtil.getNotesPath();
        // File notesForder = new File(notesPath);
        //
        // NoteForder forderRoot = new NoteForder();
        // forderRoot.name = "我的文件夹";
        // forderRoot.id = notesPath;
        // forderTreeRootNode.setUserObject(forderRoot);
        //
        // List<File> forders = FileUtil.listForders(notesForder);
        //
        // Map<File, DefaultMutableTreeNode> fileNodeMap = new HashMap<File,
        // DefaultMutableTreeNode>();
        // fileNodeMap.put(notesForder, forderTreeRootNode);
        //
        // for (File forder : forders) {
        //
        // String forderPath = forder.getAbsolutePath();
        // String canonicalPath =
        // forderPath.substring(forderPath.indexOf(notesPath) +
        // notesPath.length());
        //
        // String[] array = canonicalPath.split(File.separator);
        //
        // File temp = new File(notesPath);
        // DefaultMutableTreeNode tempNode = null;// 第一层都是从根节点开始的
        //
        // for (String value : array) {
        //
        // if (value.trim().length() == 0) {
        // continue;
        // }
        //
        // if (fileNodeMap.containsKey(temp)) {
        // tempNode = fileNodeMap.get(temp);
        // }
        //
        // temp = new File(temp, value);
        //
        // // 如果map里面有当前目录，不进行处理
        // if (fileNodeMap.containsKey(temp)) {
        // continue;
        // }
        //
        // NoteForder noteForder = new NoteForder();
        // noteForder.id = temp.getAbsolutePath();
        // noteForder.name = temp.getName();
        // noteForder.lastUpdateTime = new Date(temp.lastModified());
        // result.add(noteForder);
        //
        // DefaultMutableTreeNode node1 = new
        // DefaultMutableTreeNode(noteForder);
        // tempNode.add(node1);
        // tempNode = node1;
        //
        // fileNodeMap.put(temp, tempNode);
        // }
        // }
        //
        // return result;
    }

    public List<Note> loadNotes(NoteForder forder) {

        File file = new File(forder.id);

        File[] files = file.listFiles();

        List<Note> result = new ArrayList<Note>();

        if (files == null) {
            return result;
        }

        for (File tempFile : files) {
            if (!tempFile.isFile()) {
                continue;
            }

            Note note = new Note();
            note.id = tempFile.getAbsolutePath();
            note.name = tempFile.getName();
            note.noteForder = forder;
            note.lastUpdateTime = new Date(tempFile.lastModified());
            result.add(note);
        }

        return result;
    }

    public List<NoteForder> loadForders(NoteForder forder) {

        File file = new File(forder.id);

        File[] files = file.listFiles();

        List<NoteForder> result = new ArrayList<NoteForder>();
        if (files == null) {
            return result;
        }

        for (File tempFile : files) {
            if (!tempFile.isDirectory()) {
                continue;
            }

            NoteForder noteForder = new NoteForder();
            noteForder.name = tempFile.getName();
            noteForder.parent = forder;
            noteForder.lastUpdateTime = new Date(tempFile.lastModified());
            result.add(noteForder);
        }

        return result;
    }

    public NoteForder addForder(String pid) {
        NoteForderDao forderDao = new NoteForderDao();
        forderDao.setConnection(SwingNoteApplication.dataSource);
        return forderDao.createNoteForder(pid);
    }

    public void deleteNoteForder(String id) {
        NoteForderDao forderDao = new NoteForderDao();
        forderDao.setConnection(SwingNoteApplication.dataSource);

        NoteForder forder = new NoteForder();
        forder.pid = id;

        int count = forderDao.count(forder);

        forderDao.delete(id);
    }
}
